import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;

public class GenerateQRCode {

	public static void readApkLog(String path, String fileName) {
		List<String> jsons = readFileByLines(fileName);
		if (jsons.size() == 0) {
			return;
		}
		List<QRCodeData> qrCodeDatas = new ArrayList<>();
		for (int i = 0; i < jsons.size(); i++) {
			String jsonString = jsons.get(i);
			try {
				QRCodeEntity qrCodeEntity = JSON.parseObject(jsonString,
						QRCodeEntity.class);
				if (qrCodeEntity != null && qrCodeEntity.data != null
						&& !isEmpty(qrCodeEntity.data.appQRCodeURL)) {
					qrCodeDatas.add(qrCodeEntity.data);
				}
			} catch (Exception e) {
				System.out.println("解析json失败");
				e.printStackTrace();
			}
		}
		downloadPicture(qrCodeDatas, path);
	}

	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static List<String> readFileByLines(String fileName) {
		System.out.println("正在读取" + fileName + "文件....................");
		System.out.println("*******************************************");
		File file = new File(fileName);
		List<String> jsons = new ArrayList<>();
		if (!file.exists()) {
			return jsons;
		}
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				jsons.add(tempString.trim());
			}
			reader.close();
		} catch (IOException e) {
			System.out.println("读取" + fileName + "文件失败!!!");
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return jsons;
	}

	/**
	 * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
	 *
	 * @param input
	 * @return boolean
	 */
	public static boolean isEmpty(String input) {
		if (input == null || "".equals(input) || "null".equals(input))
			return true;

		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return false;
			}
		}
		return true;
	}

	public static void downloadPicture(List<QRCodeData> qrCodeDatas,
									   String directory) {
		URL url = null;

		for (QRCodeData qrCodeData : qrCodeDatas) {
			try {
				url = new URL(qrCodeData.appQRCodeURL);
				System.out.println(qrCodeData.appName + ": https://www.pgyer.com/" + qrCodeData.appShortcutUrl);
				DataInputStream dataInputStream = new DataInputStream(
						url.openStream());
				String imageName = qrCodeData.appName + ".jpg";
				FileOutputStream fileOutputStream = new FileOutputStream(
						new File(directory, imageName));

				byte[] buffer = new byte[1024];
				int length = -1;

				while ((length = dataInputStream.read(buffer)) > 0) {
					fileOutputStream.write(buffer, 0, length);
				}
				dataInputStream.close();
				fileOutputStream.close();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
